package com.familydoctor.init;

import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.familydoctor.entity.Category;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @program: tool
 * @description:
 * @author: tmz
 * @create: 2020-10-10 15:58
 */
public class CategoryCache {
    private List<Category> category = new ArrayList<>();
    public CategoryCache(){
        init();
    }
    public  List<Category> getCategory() {
        return category;
    }
    public CategoryCache setCategory(List<Category> category) {
        this.category = category;
        return this;
    }
    public void init(){
        List<Category> category = null;
        try {
            category =  Db.use().find(Entity.create("Category"), Category.class);
            setCategory(category);
        }catch (Exception e) {
        }
    }
    public static Integer getCategoryId(List<Category> category,String cname){
        return category.stream().filter(item-> StrUtil.equals(item.getName(),cname)).collect(Collectors.toList()).get(0).getId();
    }
    public static String getCategoryName(List<Category> category,Integer id){
        return category.stream().filter(item-> id.equals(item.getId())).collect(Collectors.toList()).get(0).getName();
    }
}
